/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.quickorm.sample;

import com.quickorm.config.Database;
import com.quickorm.core.QuickormTemplate;
import com.quickorm.sample.dao.UserDao;
import com.quickorm.sample.daoimpl.UserDaoImpl;
import com.quickorm.sample.service.UserService;
import com.quickorm.sample.serviceimpl.UserServiceImpl;
import java.io.IOException;
import javax.sql.DataSource;

/**
 *
 * @author aaa
 */
public class Program {

    public static DataSource dataSource;
    public static UserDao userDao;
    public static UserService userService;
    public static QuickormTemplate quickormTemplate;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Preparing DataSource......");
        com.mchange.v2.c3p0.ComboPooledDataSource dataSourceImpl = new com.mchange.v2.c3p0.ComboPooledDataSource();
        try {
            dataSourceImpl.setDriverClass("com.mysql.jdbc.Driver");
            dataSourceImpl.setJdbcUrl("jdbc:mysql://192.168.0.221:3306/ssmp_client?rewriteBatchedStatements=true&useUnicode=yes&characterEncoding=UTF-8");
            dataSourceImpl.setUser("root");
            dataSourceImpl.setPassword("xinheixia");
            dataSource = dataSourceImpl;
        } catch (Exception ex) {
            ex.printStackTrace();
            return;
        }

        quickormTemplate = new QuickormTemplate(dataSource);
        quickormTemplate.setDatabase(Database.MYSQL);
        quickormTemplate.setShowSql(true);
        quickormTemplate.setShowSqlLogLevel("INFO");

        System.out.println("Preparing Dao......");
        UserDaoImpl userDaoImpl = new UserDaoImpl();
        userDaoImpl.setQuickormTemplate(quickormTemplate);
        userDao = userDaoImpl;

        System.out.println("Preparing Service......");
        UserServiceImpl userServiceImpl = new UserServiceImpl();
        userServiceImpl.setUserDao(userDao);
        userService = userServiceImpl;

        try {
            userService.deleteUser("JUSTFORINIT");
        } catch (RuntimeException ex) {
        }

        while (true) {
            System.out.println();
            System.out.println("Welcome to run quickorm-sample.Please select what you want to do:");
            System.out.println("----------------------------------------------------------------");
            System.out.println("1.Test SELECT/INSERT/DELETE Feature");
            System.out.println("2.Test QuickOrm&JDBC Normal Insert Performance(may be use about 1000 seconds)");
            System.out.println("3.Test QuickOrm&JDBC Batch Insert Performance");
            System.out.println("0.Exit");

            while (true) {
                try {
                    int ReadKey = System.in.read();
                    if (ReadKey == '1') {
                        Menu1.execute();
                    } else if (ReadKey == '2') {
                        Menu2.execute(100000);
                    } else if (ReadKey == '3') {
                        Menu3.execute(100000);
                    } else if (ReadKey == '4') {
                    } else if (ReadKey == '0') {
                        return;
                    } else {
                        continue;
                    }
                } catch (IOException ex) {
                    break;
                }
            }
        }
    }
}
